<template>
  <div>
    <el-form ref="form" inline :model="form" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="姓名:" prop="userName">
            <el-input
              v-model="form.userName"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="handleSearch"
              >搜索</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              @click="handleReset"
              >清空</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div>
      <div class="absence-total">请假查询：{{ total }}</div>
      <el-table border :data="tableData" style="width: 95%" max-height="400">
        <el-table-column prop="userName" label="姓名" align="center" />
        <el-table-column prop="startDate" label="开始时间" align="center" />
        <el-table-column prop="endDate" label="结束时间" align="center" />
        <el-table-column prop="remarks" label="备注" align="center" />
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="handleEdit(scope.row)"
              type="text"
              size="small"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageInfo.pageSize"
        :total="pageInfo.total"
        @current-change="handleChangeCurrent"
      />
    </div>
    <el-dialog title="请假" :visible.sync="visible" width="30%">
      <div class="dialog-row">
        姓名：
        <el-input
          style="width: 50%"
          v-model="currentInfo.userName"
          :disabled="true"
        />
      </div>
      <div class="dialog-row">
        开始时间：
        <el-date-picker
          v-model="absenceForm.startDate"
          type="datetime"
          placeholder="选择日期时间"
          size="mini"
        >
        </el-date-picker>
      </div>
      <div class="dialog-row">
        结束时间：
        <el-date-picker
          v-model="absenceForm.endDate"
          type="datetime"
          placeholder="选择日期时间"
          size="mini"
        >
        </el-date-picker>
      </div>
      <div class="dialog-row">
        备注：
        <el-input style="width: 50%" v-model="absenceForm.remarks" />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleDialogCancel">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirem"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAbsence, updateAbsence } from "../../api/absence";

export default {
  name: "HrAbsenceQuery",
  data() {
    return {
      total: 0,
      form: {
        userName: "",
      },
      tableData: [
        {
          userName: "",
          startDate: "",
          endDate: "",
          remarks: "",
        },
      ],
      pageInfo: {
        current: 1,
        pageSize: 10,
      },
      visible: false,
      currentInfo: {},
      absenceForm: {
        userName: "",
        startDate: "",
        endDate: "",
        remarks: "",
      },
    };
  },
  created() {
    this.getAbsenceList();
  },
  methods: {
    async getAbsenceList() {
      const { current, pageSize } = this.pageInfo;
      const res = await getAbsence({ ...this.form, current, pageSize });
      const { data, pageInfo } = res?.data;
      data?.forEach((element) => {
        let startDate = new Date(Number(element.startDate));
        element.startDate = startDate.toLocaleString();
        let endDate = new Date(Number(element.endDate));
        element.endDate = endDate.toLocaleString();
      });
      this.tableData = data;
      this.pageInfo = pageInfo;
      this.total = pageInfo.total;
    },
    handleChangeCurrent(index) {
      this.pageInfo.current = index;
    },
    handleSearch() {
      this.getAbsenceList();
    },
    handleReset() {
      this.$refs["form"].resetFields();
      this.getAbsenceList();
    },
    handleEdit(info) {
      this.visible = true;
      this.currentInfo = info;
      Object.keys(this.absenceForm).forEach((item) => {
        this.absenceForm[item] = info[item];
      });
    },
    async handleDialogConfirem() {
      let startDate = new Date(this.absenceForm.startDate);
      this.absenceForm.startDate = startDate.getTime();
      let endDate = new Date(this.absenceForm.endDate);
      this.absenceForm.endDate = endDate.getTime();
      const res = await updateAbsence({
        ...this.absenceForm,
        id: this.currentInfo.id,
      });
      const { code } = res?.data;
      if (code === 0) {
        this.$message({
          message: "更新成功",
          type: "success",
        });
        this.getAbsenceList();
      } else {
        this.$message({
          message: "更新失败",
          type: "error",
        });
      }
      this.visible = false;
    },
    handleDialogCancel() {
      this.visible = false;
    },
  },
};
</script>
<style lang="less" scoped>
.absence-total {
  margin-bottom: 10px;
}
.dialog-row {
  display: flex;
  align-items: center;
  margin-top: 20px;
}
.dialog-row:first-child {
  margin-top: 0px;
}
/deep/ .el-icon-date {
  display: none;
}
</style>
